﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace University.Workshop.DataAccess
{
    public class DatabaseManager //: IDisposable
        {
           //LO HE COMENTADO POR QUE ESTE GESTOR TIENE MUCHOS MAS WARNINGS QUE EL MIO :/
           /* private static DatabaseManager instance = null;

            private string myConnectionString = "Data Source=.\\sqlexpress; Initial Catalog=Pharmacy;Integrated Security=true";
            private SqlConnection myConnection;
            private SqlCommand mySelectCommand;
            private SqlCommand myCommand;
            private SqlDataAdapter myDataAdapter;

            public SqlConnection MyConnection
            {
                get { return myConnection; }
            }
            public string MyConnectionString
            {
                get { return myConnectionString; }
                set { myConnectionString = value; }
            }

            public DatabaseManager()
            {
                myConnection = new SqlConnection(myConnectionString);

            }

            public static DatabaseManager Instance
            {
                get
                {
                    if (instance == null)
                        instance = new DatabaseManager();
                    return instance;
                }
            }

            public DataSet EjectSelectQuery(string selectQuery)
            {
                DataSet myDataSet = new DataSet();
                try
                {
                    myConnection.Open();
                    mySelectCommand = new SqlCommand(selectQuery, myConnection);
                    myDataAdapter = new SqlDataAdapter();
                    myDataAdapter.SelectCommand = mySelectCommand;
                    myDataAdapter.Fill(myDataSet);
                    if (myDataSet != null)
                    {
                        if (myDataSet.Tables.Count != 0)
                        {
                            if (myDataSet.Tables[0].Rows.Count != 0)
                            {
                                return myDataSet;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return myDataSet;
                    }
                }
                catch (Exception)
                {
                    return null;
                }

                finally
                {
                    if (myConnection.State != ConnectionState.Closed)
                        myConnection.Close();
                }
            }


            public int EjectQuery(string query)
            {
                int registrosAfectados = 0;
                try
                {
                    myConnection.Open();
                    myCommand = new SqlCommand(query, myConnection);
                    registrosAfectados = myCommand.ExecuteNonQuery();
                    return registrosAfectados;
                }
                catch (Exception)
                {
                    throw new Exception();
                }
                finally
                {

                    if (myConnection.State != ConnectionState.Closed)
                        Dispose(true);
                    else
                        Dispose(false);

                    
                }
            }
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    // dispose managed resources
                    myConnection.Close();
                }
                // free native resources
            }

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }*/

        }
    
}
